/**
 * 
 */
package com.obaamilos.msstore;

//import org.mindswap.pellet.jena.PelletReasonerFactory;

import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
//import com.clarkparsia.sparqlowl.parser.arq.ARQTerpParser;
//import com.clarkparsia.sparqlowl.parser.arq.TerpSyntax;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
//import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.tdb.TDBFactory;
//import com.hp.hpl.jena.tdb.base.file.Location;

/**
 * @author Pedro R. Oliveira
 *
 */

public class QuerySparql {
	   
    private String query;
	private String diretorio;
    private String result;
    private int tipoResposta; // 0 = Texto, 1 = XML

    //public static Model MODEL_JENA;
    //public static Model DATA_BASE_MODEL;
    //public static Location LOCALIZACAO;

    public int getTipoResposta() {
		return tipoResposta;
	}

	public void setTipoResposta(int tipoResposta) {
		this.tipoResposta = tipoResposta;
	}

	public QuerySparql() {
		// TODO Auto-generated constructor stub
	}

    public QuerySparql(String query, String diretorio, int tresposta) {
        this.query = query;
        this.diretorio = diretorio;
        this.tipoResposta = tresposta;
    }

    public void run() {
        //Model m = null;

        try  {  
            //ARQTerpParser.registerFactory();
            
            // Pellet Reasoner
            //  m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC, datasetModel); //TDB
            System.out.println("Usando TDB em: " + diretorio);
            
            Dataset ds = TDBFactory.createDataset(diretorio) ;
            Model model = ds.getDefaultModel() ;
            
            //DATA_BASE_MODEL = TDBFactory.cre .createModel(diretorio);
            //MODEL_JENA = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC,DATA_BASE_MODEL); //ARQUIVO

            //m = MODEL_JENA;

            // Now read the query file into a query object
            // Important: specifying that the query is in Terp syntax
            //com.hp.hpl.jena.query.Query q = QueryFactory.create(getQuery(), TerpSyntax.getInstance());
            com.hp.hpl.jena.query.Query q = QueryFactory.create(getQuery());

            // Create a SPARQL-DL query execution for the given query and
            // ontology model
            QueryExecution qe = SparqlDLExecutionFactory.create(q, model);

            // We want to execute a SELECT query, do it, and return the result set
            ResultSet rs = qe.execSelect();
            
            if (tipoResposta > 0) {
                // return result as XML
                setResult(ResultSetFormatter.asXMLString(rs));
                //setResult(ResultSetFormatter.asText(rs));
            }
            else {
                ResultadoQuery mresul = new ResultadoQuery(rs);
            	setResult(mresul.toString());
            }
            
            //System.out.println("R: " + getResult());

            // Close the dataset.
            ds.close();
        } catch (Exception e) {
            setResult("Erro:" +" Error query executing "+e.getMessage());
            System.out.println("Erro Query: " + e.getMessage());
        } finally {
            //close connection with TDB
            //DATA_BASE_MODEL.close();
            //MODEL_JENA.close(); 
        }
    }

    public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getDiretorio() {
		return diretorio;
	}

	public void setDiretorio(String diretorio) {
		this.diretorio = diretorio;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
}
